#include "ConditionalTranslatingObject.h"
#include "include.h"

ConditionalTranslatingObject::ConditionalTranslatingObject(TextureManager * textureManager, Vertex position)
: TranslatingObject(textureManager, position)
{
	conditionalStatement = "IF(EQUAL((INT,1),(INT,1)),~0,~1)";
	CreatePropertyMenu();
	propertyMenu->SetFormContent(1,"100");
	propertyMenu->Hide();
}
ConditionalTranslatingObject::ConditionalTranslatingObject(TextureManager * textureManager, 
	float textureRotation, VertexList * vertices, Material * material, Vector2D position, VertexList * path, float mass, float speed, char * conditionalStatement)
:TranslatingObject(textureManager, textureRotation, vertices, material, position, path, mass, speed)
{
	this->conditionalStatement = conditionalStatement;
	CreatePropertyMenu();
	float speedCheck = speed;
	int size = 0;
	while(speedCheck > 1)
	{
		size++;
		speedCheck /= 10;
	}
	size += 6;
	char * speedString = (char*)malloc(sizeof(char)*size);
	sprintf(speedString, "%.3f", speed);
	propertyMenu->SetFormContent(1,speedString);
	propertyMenu->Hide();
}
ConditionalTranslatingObject::~ConditionalTranslatingObject()
{
	if(conditionalStatement)
	{
		free(conditionalStatement);
		conditionalStatement = NULL;
	}
}
	
char * ConditionalTranslatingObject::GetTag()
{
	printf("Getting border tag\n");
	char * points = vertices->GetRelativeTag(position.GetPosition());
	char * materialName = material->name;
	char * pathTag = path->GetVertices()->GetBackwardsTag();
	char * mass = propertyMenu->GetFormContent(0);
	char * speed = propertyMenu->GetFormContent(1);
	char * conditionalText = propertyMenu->GetFormContent(2);
	char * tag = (char *)malloc(strlen(points)+strlen(materialName)+strlen(pathTag)+strlen(mass)+strlen(speed)+strlen(conditionalText)+64);
	int numPath = path->GetVertices()->GetLength();
	sprintf(tag, "<object,conditionaltranslatingobject,%s,%s,%.3f,%d,%s,%s,%s,%d,%s>", conditionalText,materialName,textureRotation,vertices->GetLength(),points, speed,mass,numPath, pathTag);
	delete points;
	free(mass);
	free(speed);
	free(conditionalText);
	return tag;	
}
void ConditionalTranslatingObject::CreatePropertyMenu()
{
	printf("ConditionalTranslatingObject::CreatePropertyMenu\n");
	if(propertyMenu)
	{
		delete(propertyMenu);
		propertyMenu = NULL;
	}
	propertyMenu = new FormMenu(textureManager, "3,Condition,6,Speed,1,Mass,1", PROPERTY_MENU_POSITION, 0);
	float massCheck = mass;
	int size = 0;
	while(massCheck > 1)
	{
		size++;
		massCheck /= 10;
	}
	size += 6;
	char * massString = (char*)malloc(sizeof(char)*size);
	sprintf(massString, "%.3f", mass);
	propertyMenu->SetFormContent(0,massString);
	propertyMenu->SetFormContent(2,conditionalStatement);
}